package com.yy.yyui.bitmap
{
	import com.siku.manor.view.common.UM;
	import com.siku.manor.view.component.GT;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;

	public class LoadBitmap extends Bitmap
	{
		private var loader:Loader;
		public function LoadBitmap(src:Object,expectedWidth:Number = 64,expectedHeight:Number = 64, pixelSnapping:String="auto", smoothing:Boolean=false)
		{
			if(src is BitmapData){
				super(BitmapData(src),pixelSnapping,smoothing);
			}else if(src is URLRequest){
				this.bitmapData = UM.createPreviewBitmap(expectedWidth,expectedHeight).bitmapData;
				this.pixelSnapping = pixelSnapping;
				this.smoothing = smoothing;

				loader = new Loader();
				loader.load(URLRequest(src));
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,addImage);	
			}else{
				this.bitmapData = GT.copyBitmap(UM.visualize(src)).bitmapData;
				this.pixelSnapping = pixelSnapping;
				this.smoothing = smoothing;
			}
		}
		


		private function addImage(e:Event):void{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,addImage);	
			var dobj:DisplayObject = DisplayObject(e.target.loader.content);
			var oldData:BitmapData = this.bitmapData;
			if(!(dobj is Bitmap)){
				dobj = GT.copyBitmap(dobj);
			}
			if(dobj is Bitmap){
				this.bitmapData = Bitmap(dobj).bitmapData;
				this.width = oldData.width;
				this.height = oldData.height;
			}
			oldData.dispose();
		}
		
	}
}